﻿using CozeDomain.IRepositories;
using CozeDomain.Entities;
using CozeDto.SysDicTypeDtos;
using CozeDto.SysDicDtos;
using D.UtilFreesqlOrm;

namespace CozeDomain.Repositories
{
    /// <summary>
    /// 
    /// </summary>
    public class SysDicTypeEntityRepository : BaseRepository<SysDicTypeEntity>, ISysDicTypeEntityRepository
    {
        public SysDicTypeEntityRepository(IFreeSql db) : base(db)
        {
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="query"></param>
        /// <param name="async"></param>
        /// <returns></returns>
        public async Task<Tuple<long, List<SysDicTypeEntity>>> GetPagesAsync(ReqQuerySysDicTypePage query)
        {
            var sqlQuery = _db.Select<SysDicTypeEntity>()
                                .WhereIf(!string.IsNullOrEmpty(query.Name), m => m.Name.Contains(query.Name))
                                .WhereIf(!string.IsNullOrEmpty(query.UniqueCode), m => m.UniqueCode.Contains(query.UniqueCode))
                                .WhereIf(query.Status != null, m => m.Status == query.Status)
                                .OrderByDescending(m => m.CreateTime);


            var dbres = await sqlQuery.Count(out var totalCount)
                                                        .Page(query.PageNo, query.PageSize)
                                                        .ToListAsync<SysDicTypeEntity>();

            return new Tuple<long, List<SysDicTypeEntity>>(totalCount, dbres);
        }

        /// <summary>
        /// 下拉选择框查询
        /// </summary>
        /// <returns></returns> 
        public async Task<List<RepSelect>> QueryDicTypeSelect()
        {
            return await _db.Select<SysDicTypeEntity>().Where(m => m.Status.Value).ToListAsync(m => new RepSelect
            {
                Id = m.Id,
                Value = m.UniqueCode,
                Name = m.Name
            });
        }
    }
}
